package com.myTest.service.impl;

import com.myTest.annotation.Autowired;
import com.myTest.annotation.Service;
import com.myTest.annotation.Transactional;
import com.myTest.dao.AccountDao;
import com.myTest.dao.impl.JdbcAccountDaoImpl;
import com.myTest.factory.BeanFactory;
import com.myTest.pojo.Account;
import com.myTest.service.TransferService;
import com.myTest.utils.ConnectionUtils;
import com.myTest.utils.TransactionManager;

@Service("transferService")
public class TransferServiceImpl implements TransferService{

    //仅声明dao接口
    @Autowired
    private AccountDao accountDao;

    @Override
    @Transactional
    public void transfer(String fromCardNo, String toCardNo, int money) throws Exception {
      /*  try {
            //开启事务（关闭事务的自动提交）
            TransactionManager.getInstance().beginTransaction();*/

            Account from = accountDao.queryAccountByCardNo(fromCardNo);
            Account to = accountDao.queryAccountByCardNo(toCardNo);
            from.setMoney(from.getMoney()-money);
            to.setMoney(to.getMoney()+money);
            accountDao.updateAccountByCardNo(from);
           // int i= 1/0;
            accountDao.updateAccountByCardNo(to);

       /*     //提交事务
            TransactionManager.getInstance().commit();
        }catch (Exception e){
            e.printStackTrace();
            //回滚事务
            TransactionManager.getInstance().rollback();

            throw e;
        }*/

    }
}
